<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class Payment extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        //钱包
        $wallet = logic('Wallet')->getName();
        $this->wallet = $wallet;
        View::assign('wallet',$wallet);
    }
    public function index(){
        //$map = $this->_search();
        $wallet_arr = logic('Wallet')->getName();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['r.user_id'] = input('user_id');
            }

            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['r.w_time'] = ['between',[$time_start,$time_end]];
            }
            if(input('?param.status') && input('status')!='all'){
                $map['r.status'] = input('status');
            }
            $count = Db::name('payment')->alias('r')->where($map)->count();

            $field = 'r.*';
            $list = Db::name('payment')
                ->alias('r')
                ->field($field)
                ->where($map)
                ->order('r.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['status_str'] = $v['status']==1?'已支付':'未支付';
                $v['paytype_str'] = $v['paytype']=='weixin'?'微信':'支付宝';
                $v['order_str'] = $v['table']=='order'?'订单':'充值';
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }

        return View::fetch();
    }

    /**
     * 导出
     */
    public function excel()
    {
        $wallet_arr = logic('Wallet')->getName();
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['r.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['r.username'] = input('username');
        }
        if(input('money_type')){
            $map['r.money_type'] = input('money_type');
        }
        if(input('time_start') && input('time_end')){
            $time_start = strtotime(input('time_start'));
            $time_end = strtotime(input('time_end'));
            $map['r.w_time'] = ['between',[$time_start,$time_end]];
        }
        $field = 'r.*';
        $count = Db::name('recharge_log')
            ->alias('r')
            ->field($field)
            ->where($map)
            ->count();
        $list = Db::name('recharge_log')
            ->alias('r')
            ->field($field)
            ->where($map)
            ->page($page,$limit)
            ->order('r.id desc ')
            ->select();
        $title = ['ID','会员ID','会员编号','充值钱包','充值金额','备注','管理员ID','充值时间'];
        $data = [];
        foreach($list as $k=>$v){
            $v['wallet_name'] = $wallet_arr[$v['money_type']];
            $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            $list[$k] = $v;
            $arr = [
                $v['id'],$v['user_id'],$v['username'],$v['wallet_name'],$v['money'],$v['memo'],$v['admin_id'],$v['w_time']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
        $this->export_excel($title,$data);
    }

    public function del(){
        $id_arr = input('ids/a');
        Db::startTrans();
        Db::name('recharge_log')->where(['id'=>['in',$id_arr]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'删除成功!']);
    }
}